﻿using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class TargetSelect_InputUnit : TargetSelect
    {
        public override ETargetSelectType selectType => ETargetSelectType.InputUnit;
        public override bool selectByUnit => true;

        protected override void OnInitialize(TargetSelectConfig rConfig)
        {
            base.OnInitialize(rConfig);
            if (rConfig.customConfig is TargetSelect_InputUnit_ConfigData rData)
            {
                this.targetDisRange = new LFloat(true, rData.range);
            }
        }

        protected override void OnInitialize(IReadOnlyList<int> rTabConfig)
        {
            base.OnInitialize(rTabConfig);
            if (rTabConfig.Count > 0)
            {
                this.targetDisRange = new LFloat(true, rTabConfig[0]);
            }
        }

        public override void TrySelect(SelectParam rParam, LVector2 rSelfPos, ref List<Unit> rTargets)
        {
            var rTarget = rParam.unit;
            if (rTarget != null)
            {
                var rDis = (rSelfPos - rTarget.pos).sqrMagnitude;
                if (rDis <= this.targetDisRange * this.targetDisRange)
                {
                    rTargets.Add(rParam.unit);
                }
            }
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }
    }
}